﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using System.Collections.Generic;
using Meqs.Logic;

public partial class Student_Report_HistoryScore : System.Web.UI.Page
{
    protected List<StuExam> stuExam = new List<StuExam>();
    protected List<int> schoolYear = new List<int>();
    /// <summary>
    /// 本次考试的科目(统考)
    /// </summary>
    protected IList<ExamSubjecyEducation2> subjEdu = null;
    /// <summary>
    /// 本次考试的科目(校考)
    /// </summary>
    protected IList<ExamSubjecySchool2> subjSchool = null;
    /// <summary>
    /// 统考成绩列表（已经输入的）
    /// </summary>
    protected IList<GradeScoreEducation> gse = null;
    /// <summary>
    /// 校考试成绩
    /// </summary>
    protected IList<ExamDetailSchool> eds = null;
    /// <summary>
    /// 最大的科目
    /// </summary>
    protected List<ExamSubjecyEducation2> temp;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Hashtable ht = null;
            for (int i = 1; i < 13; i++)
            {
                ht = new Hashtable();
                ht.Add("StudentID", CommonScore.LogonStudent.RID);
                if (!string.IsNullOrEmpty(CommonScore.GetGradeStoreTable(i)))
                {
                    ht.Add("TableName", CommonScore.GetGradeStoreTable(i));
                    IList<ExamsEducation> eus = Container<Meqs.Logic.Interface.IExamsEducationLogic>.Instance.FindStudentExam(ht);
                    foreach (ExamsEducation u in eus)
                    {
                        StuExam st = new StuExam(1, u.RID, u.Name, u.SchoolYear, u.Grade, u.ClassType, u.CreateDate);
                        stuExam.Add(st);
                    }
                }
            }

            ht = new Hashtable();
            ht.Add("StudentID", CommonScore.LogonStudent.RID);
            ht.Add("SchoolMark", CommonScore.GetSchoolMark);
            IList<ExamsSchool> ess = Container<Meqs.Logic.Interface.IExamsSchoolLogic>.Instance.FindStudentExam(ht);
            foreach (ExamsSchool u in ess)
            {
                StuExam st = new StuExam(0, u.RID, u.Name, u.SchoolYear, u.Grade, u.ClassType,u.CreateDate);
                stuExam.Add(st);
            }

            foreach (StuExam se in stuExam)
            {
                if (!schoolYear.Contains(se.schoolYear))
                {
                    int index = 0;
                    foreach (int y in schoolYear)
                    {
                        if (se.schoolYear > y)
                        {
                            break;
                        }
                        index++;
                    }
                    schoolYear.Insert(index, se.schoolYear);
                }
            }

        }
    }
    protected void btnBind_Click(object sender, EventArgs e)
    {
        string starty = txtStart.Text;
        string starte = txtEnd.Text;
        string eduids = txteduIDS.Text;
        string edugrades = txteduGrades.Text;
        string schoolids = txtschoolIDS.Text;
        string strdateTime = txtDateTime.Text;

        Hashtable ht = new Hashtable();
        ht.Add("SubjectIDS", eduids);
        subjEdu = Container<Meqs.Logic.Interface.IExamSubjecyEducationLogic>.Instance.GetExamCause(ht);
        ht = new Hashtable();
        ht.Add("SchoolMark", CommonScore.GetSchoolMark);
        ht.Add("SubjectIDS", schoolids);
        subjSchool = Container<Meqs.Logic.Interface.IExamSubjecySchoolLogic>.Instance.GetExamCause(ht);

        temp = new List<ExamSubjecyEducation2>();
        foreach (ExamSubjecyEducation2 ee in subjEdu)
        {
            if (!Check(temp, ee.SubjectID))
            {
                temp.Add(ee);
            }
        }

        foreach (ExamSubjecySchool2 ee in subjSchool)
        {
            if (!Check(temp, ee.SubjectID))
            {
                ExamSubjecyEducation2 te = new ExamSubjecyEducation2();
                te.SubjectID = ee.SubjectID;
                te.SubjectName = ee.SubjectName;
                temp.Add(te);
            }
        }

        List<StuExam> ses = new List<StuExam>();
        string[] edids = eduids.Split(',');
        string[] edgs = edugrades.Split(',');
        string[] sids = schoolids.Split(',');
        string[] stimes = strdateTime.Split(',');
        int indexi = 0;
        foreach(string ei in edids)
        {
            if (Convert.ToInt32(ei) > 0)
            {
                StuExam s = new StuExam(1, Convert.ToInt32(ei), "", 0, Convert.ToInt32(edgs[indexi]), 0,new DateTime(Convert.ToInt64( stimes[indexi])));

                ses.Add(s);
            }
            indexi++;
        }

        indexi = 0;
        foreach (string ei in sids)
        {
            if (Convert.ToInt32(ei) > 0)
            {
                StuExam s = new StuExam(0, Convert.ToInt32(ei), "", 0, 0, 0, new DateTime(Convert.ToInt64(stimes[indexi])));

                ses.Add(s);
            }
            indexi++;
        }
        ListSort<StuExam> sort = new ListSort<StuExam>(ses, "CreateTime", true);
        Repeater1.DataSource = sort.Sort();
        Repeater1.DataBind();
    }

    protected string GetHeader()
    {
        int w = (800 - 320);
        string header = "<th width='240px' style='text-align:left;padding-left: 5px;'>考试名</th><th width='80px' style='text-align:left;padding-left: 5px;'>考试时间</th>";

        if (temp != null)
        {
            w = (temp.Count == 0 ? 0 : w / (temp.Count + 1));
            foreach (ExamSubjecyEducation2 ese in temp)
            {
                header += "<th width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + ese.SubjectName + "</th>";
            }
            if (subjEdu.Count >= 0)
            {
                header += "<th width='" + w + "px' style='text-align:right;padding-right: 5px;'>总分</th>";
            }
        }


        return header;
    }
    protected string GetExaminfo(int type,int examid,int grade,DateTime dtime)
    {
        string examinfo = "";
        double fullscore = 0;
        int w = (800 - 320);

        w = (temp.Count == 0 ? 0 : w / (1 + temp.Count));
        if (type == 1)
        {
            
            ExamsEducation exam = Container<Meqs.Logic.Interface.IExamsEducationLogic>.Instance.FindById(examid);
            examinfo += "<td width='240px' style='text-align:left;padding-left: 5px;'>" + exam.Name + "</td>";
            examinfo += "<td width='80px' style='text-align:left;padding-left: 5px;'>" + dtime.ToString("yy-MM-dd") + "</td>";
            Hashtable ht = new Hashtable();
            ht.Add("ExamID", examid);
            ht.Add("StudentID", CommonScore.LogonStudent.RID);
            ht.Add("TableName", CommonScore.GetGradeStoreTable(grade));
            IList<GradeScoreEducation> gs = Container<Meqs.Logic.Interface.IGradeScoreEducationLogic>.Instance.FindAll(ht);

            foreach(ExamSubjecyEducation2 ee in temp)
            {
                string score = GetTongKaoScore(ee.SubjectID, gs);
                if (score != "null")
                {
                    examinfo += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + score + "</td>";
                    fullscore += Convert.ToDouble(score);
                }
                else
                {
                    examinfo += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'></td>";
                }
                
            }
        }
        else
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", examid);
            ht.Add("SchoolMark", CommonScore.GetSchoolMark);
            IList<ExamsSchool> exam = Container<Meqs.Logic.Interface.IExamsSchoolLogic>.Instance.FindAll(ht);
            if (exam.Count > 0)
            {
                examinfo += "<td width='160px' style='text-align:left;padding-left: 5px;'>" + exam[0].Name + "</td>";
                examinfo += "<td width='80px' style='text-align:left;padding-left: 5px;'>" + dtime.ToString("yy-MM-dd") + "</td>";
            }
            else
            {
                examinfo += "<td width='160px' style='text-align:left;padding-left: 5px;'></td>";
                examinfo += "<td width='80px' style='text-align:left;padding-left: 5px;'></td>";
            }
            ht = new Hashtable();
            ht.Add("ExamID", examid);
            ht.Add("StudentID", CommonScore.LogonStudent.RID);
            ht.Add("SchoolMark", CommonScore.GetSchoolMark);
            IList<ExamDetailSchool> gs = Container<Meqs.Logic.Interface.IExamDetailSchoolLogic>.Instance.FindAll(ht);

            foreach (ExamSubjecyEducation2 ee in temp)
            {
                string score = GetXiaoKaoScore(ee.SubjectID, gs);
                if (score != "null")
                {
                    examinfo += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + score + "</td>";
                    fullscore += Convert.ToDouble(score);
                }
                else
                {
                    examinfo += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'></td>";
                }

            }

        }
        examinfo += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + fullscore + "</td>";
        return examinfo;
    }
    /// <summary>
    /// 获得某学生的校考分数
    /// </summary>
    /// <param name="stuId"></param>
    /// <param name="subjId"></param>
    /// <returns></returns>
    private string GetXiaoKaoScore(int subjId, IList<ExamDetailSchool> gs)
    {
        foreach (ExamDetailSchool e in gs)
        {
            if ( e.SubjectID == subjId)
            {
                return (e.Score == null) ? "null" : e.Score.ToString();
            }
        }
        return "null";
    }
    /// <summary>
    /// 获得某学生的统考分数
    /// </summary>
    /// <param name="stuId"></param>
    /// <param name="subjId"></param>
    /// <returns></returns>
    private string GetTongKaoScore( int subjId, IList<GradeScoreEducation> gs)
    {

        foreach (GradeScoreEducation g in gs)
        {
            if ( g.SubjectID == subjId)
            {
                return (g.Score == null) ? "null" : g.Score.ToString();
            }
        }
        return "null";
    }
    private bool Check(List<ExamSubjecyEducation2> temp ,int subjid)
    {
        foreach(ExamSubjecyEducation2 t in temp)
        {
            if (t.SubjectID == subjid)
            {
                return true;
            }
        }
        return false;
    }
    protected class StuExam
    {
        public StuExam(int t, int i, string n, int y, int g, int ct,DateTime d)
        {
            examType = t;
            examID = i;
            examName = n;
            schoolYear = y;
            grade = g;
            classType = ct;
            dateTime = d;
        }
        public int examType;
        public int examID;
        public string examName;
        public int schoolYear;
        public int grade;
        public int classType;
        public DateTime dateTime;

        public DateTime CreateTime
        {
            get { return dateTime; }
            set { dateTime = value; }
        }
        public int ExamType
        {
            get { return examType; }
            set { examType= value; }
        }

        public int ExamID
        {
            get { return examID; }
            set { examID = value; }
        }

        public string ExamName
        {
            get { return examName; }
            set { examName = value; }
        }

        public int SchoolYear
        {
            get { return schoolYear; }
            set { schoolYear = value; }
        }

        public int Grade
        {
            get { return grade; }
            set { grade = value; }
        }

        public int ClassType
        {
            get { return classType; }
            set { classType = value; }
        }
    }
}
